package com.dxy.hms.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.dxy.hms.base.UnifiedException;
import com.dxy.hms.manager.DictCacheManager;
import com.dxy.hms.model.entity.Room;
import com.dxy.hms.mapper.RoomMapper;
import com.dxy.hms.model.excel.RoomExcel;
import com.dxy.hms.service.RoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 房间表 服务实现类
 * </p>
 *
 * @author dxy
 * @since 2024-10-29
 */
@Slf4j
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    @Override
    public void add(Room entity) {
        Assert.notBlank(entity.getRoomCode(),"房间编号不能为空");
        Assert.notNull(entity.getRoomType(),"房间类型不能为空");
        Assert.notBlank(entity.getRoomStatus(),"房间状态不能为空");
        if (baseMapper.selectCount(new LambdaQueryWrapper<Room>()
                .eq(Room::getRoomCode, entity.getRoomCode())) > 0) {
            throw new UnifiedException(500,"房间编号{}已存在", entity.getRoomCode());
        }
        baseMapper.insert(entity);
    }

    @Override
    public void updById(Room entity) {
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void delByIds(List<Long> ids) {
        ids.forEach(id -> baseMapper.delById(id));
    }

    @Override
    public Room getDetail(Long id) {
        return baseMapper.findRoomDetail(id);
    }

    @Override
    public IPage<Room> getPage(Long roomTypeId, String roomStatus, Long pageNum, Long pageSize) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1L;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10L;
        }
        IPage<Room> page = new Page<>(pageNum, pageSize);
        return baseMapper.findRoomPage(page, roomTypeId, roomStatus);
    }

    @Override
    public List<RoomExcel> findRoomExcel(Long roomTypeId, String roomStatus) {
        List<RoomExcel> list = baseMapper.findRoomExcel(roomTypeId, roomStatus);
        if (CollUtil.isNotEmpty(list)){
            list.forEach(item -> {
                item.setRoomStatusText(DictCacheManager.INSTANCE.getDictName("room_status", item.getRoomStatus()));
            });
        }
        return list;
    }

}
